// To change this License template, choose Tools / Templates
// and edit Licenses / FanDefaultLicense.txt
//
// History:
//   01.02.2011 JSTAUDEMEYER Creation
//

//using [java]com.sleepycat.je
using [java]com.sleepycat.je::Environment as JEnvironment
using [java]com.sleepycat.je::EnvironmentConfig as JEnvironmentConfig
using [java]com.sleepycat.je::Database as JDatabase
using [java]com.sleepycat.je::DatabaseConfig as JDatabaseConfig

using [java]java.io::File as JFile

**
** Main
**
class Main
{
    **
    ** Main method
    **
    static Void main()
    {
        // Open the environment. Allow it to be created if it does not 
        // already exist.
        filename := "js_bdb/db/"
        f := File(Uri(filename),false).normalize
        echo(f.toStr())
        echo(f.exists)    
        
        JEnvironment? env := null
        JDatabase? db := null    
        
        try {     
            envConfig := JEnvironmentConfig()
            envConfig.setAllowCreate(true)
            env = JEnvironment(JFile(filename),envConfig)
            dbConfig := JDatabaseConfig()
            dbConfig.setAllowCreate(true)
            db = env.openDatabase(null,"sampleDatabase",dbConfig)      
        } finally {
            if (env!=null) {
                if (db!=null) db.close
                env.close 
            }
        }      
    }
    
}

class Database {
  
  JDatabase jDb
  
  protected new make(JDatabase jDb) {
    this.jDb = jDb
  }
 
}

class Environment  {
  
  JEnvironment jEnv
  
  new make(Uri uri, Str:Obj confMap) {
    uri.
    envConfig := config(confMap,JEnvironmentConfig())
    jEnv = JEnvironment(JFile(File(uri).toStr),envConfig)
  }
 
  Database openDatabase(Str name, Str:Obj confMap) {
    dbConfig := config(confMap,JDatabaseConfig())
    jDb := jEnv.openDatabase(null,name,dbConfig)
  }

    
  static Obj config (Str:Obj map, Obj confObj) {
      map.each |value,key| {
         setter := "set"+key.getRange(0..0).upper+key.getRange(1..-1)
         confObj.trap(setter,[value])        
      }
      return confObj  
    }
         
}
 